/**
 *
 */
package jp.seraph.jsade.perceptor;

import java.util.HashMap;
import java.util.Map;

import jp.seraph.jsade.message.MessageUtil;
import jp.seraph.jsade.sexpression.Node;

/**
 *
 *
 */
public class DefaultPerceptorBuilder implements PerceptorBuilder {
    /**
     * Perceptor名からPerceptorBuilderへのMapを受取り、PerceptorBuilderの初期化を行うコンストラクタ
     *
     * @param aBuilderMap
     */
    public DefaultPerceptorBuilder(Map<String, PerceptorBuilder> aBuilderMap){
        this.init(aBuilderMap);
    }

    /**
     *
     */
    public DefaultPerceptorBuilder(){
        Map<String, PerceptorBuilder> tBuilderMap = new HashMap<String, PerceptorBuilder>();

        tBuilderMap.put("GS", new GameStateBuilder());
        tBuilderMap.put("GYR", new GyroBuilder());
        tBuilderMap.put("HJ", new HingeJointBuilder());
        tBuilderMap.put("See", new SeeBuilder());
        tBuilderMap.put("time", new TimeBuilder());
        tBuilderMap.put("FRP", new ForceResistanceBulder());
        tBuilderMap.put("hear", new HearBulder());

        this.init(tBuilderMap);
    }

    private void init(Map<String, PerceptorBuilder> aBuilderMap){
        mBuilderMap = aBuilderMap;
    }

    private Map<String, PerceptorBuilder> mBuilderMap;

    /**
     *
     * @see jp.seraph.jsade.perceptor.PerceptorBuilder#build(jp.seraph.jsade.sexpression.Node)
     */
    public Perceptor build(Node aNode) throws IllegalArgumentException {
        if (aNode.childCount() <= 1 || aNode.getChild(0).isNode())
            throw new IllegalArgumentException();

        String tPerceptorName = aNode.getLeftString();
        PerceptorBuilder tBuilder = mBuilderMap.get(tPerceptorName);
        if (tBuilder == null)
            throw new IllegalArgumentException(MessageUtil.unsupportedPerceptor(tPerceptorName));

        return tBuilder.build(aNode);
    }

}
